﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace NoDriverPrintHelperV1d0.Entity
{
    /// <summary>
    /// 打印任务
    /// </summary>
    [XmlRoot("打印任务")]
    public class PrintTaskV1d0
    {
        [XmlAttribute("任务标识")]
        public Guid id { get; set; }
        [XmlAttribute("任务创建时间")]
        public DateTime create_time { get; set; }
        [XmlAttribute("任务自增序号")]
        public long task_index { get; set; }

        [XmlAttribute("打印进度")]
        public int print_progress { get; set; }
        [XmlAttribute("打印进度更新时间")]
        public DateTime progress_update_time { get; set; }
        [XmlElement("任务项列表")]
        public List<PrintTaskItemV1d0> PrintTaskItemList { get; set; }
        public PrintTaskV1d0()
        {
            id = Guid.NewGuid();
            create_time = DateTime.Now;
            print_progress = 0;//未打印
            progress_update_time = DateTime.Now;
            PrintTaskItemList = new List<PrintTaskItemV1d0>();
        }
        /// <summary>
        /// 从配置数据保存到xml文件
        /// </summary>
        /// <param name="data"></param>
        /// <param name="xmlFilename"></param>
        public static void SavaToXml(PrintTaskV1d0 data, string xmlFilename)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(PrintTaskV1d0));
            StreamWriter sW = new StreamWriter(xmlFilename);
            try
            {
                xmlSerializer.Serialize(sW, data);
            }
            finally
            {
                sW.Close();
            }
        }

        /// <summary>
        /// 从xml文件读取配置数据
        /// </summary>
        /// <param name="xmlFilename"></param>
        /// <returns></returns>
        public static PrintTaskV1d0 LoadFromXml(string xmlFilename)
        {
            PrintTaskV1d0 resultData = null;
            try
            {
                if (File.Exists(xmlFilename))
                {
                    StreamReader sR = new StreamReader(xmlFilename);
                    XmlSerializer xmlS = new XmlSerializer(typeof(PrintTaskV1d0));
                    resultData = (PrintTaskV1d0)xmlS.Deserialize(sR);
                    sR.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + ex.StackTrace);
            }
            return resultData;
        }
    }

    /// <summary>
    /// 打印任务项
    /// </summary>
    [XmlRoot("打印任务项")]
    public class PrintTaskItemV1d0
    {
        [XmlAttribute("任务项标识")]
        public Guid id { get; set; }
        /// <summary>
        /// 打印顺序号
        /// </summary>
        [XmlAttribute("打印顺序号")]
        public int print_sequence { get; set; }
        /// <summary>
        /// 格式名称
        /// </summary>
        [XmlElement("打印格式")]
        public PrintTextFormatV1d0 TextFormat { get; set; }
        /// <summary>
        /// 内容文本
        /// </summary>
        [XmlAttribute("内容文本")]
        public string content_text { get; set; }
        public PrintTaskItemV1d0()
        {
            id = Guid.NewGuid();
            print_sequence = 0;
            TextFormat = new PrintTextFormatV1d0();
            content_text ="";
        }
        public PrintTaskItemV1d0(int sequence)
        {
            id = Guid.NewGuid();
            print_sequence = sequence;
            TextFormat = new PrintTextFormatV1d0();
            content_text = "";
        }
    }
    [XmlRoot("打印文本格式")]
    public class PrintTextFormatV1d0
    {
        [XmlAttribute("格式标识")]
        public Guid id { get; set; }
        /// <summary>
        /// 格式名称
        /// </summary>
        [XmlAttribute("格式名称")]
        public string format_name { get; set; }
        /// <summary>
        /// 字体颜色名称
        /// </summary>
        [XmlAttribute("字体颜色名称")]
        public string font_color_name { get; set; }
        /// <summary>
        /// 文字尺寸 标注，倍宽，倍高，
        /// </summary>
        [XmlAttribute("是否倍高")]
        public bool is_double_height { get; set; }
        [XmlAttribute("是否倍宽")]
        public bool is_double_width { get; set; }
        [XmlAttribute("是否粗体")]
        public bool is_bold { get; set; }
        /// <summary>
        /// 文字编码名称 Big5 GB2312 GB18030
        /// </summary>
        [XmlAttribute("编码名称")]
        public string encoding_name { get; set; }
        /// <summary>
        /// 文字位图的像素宽度 
        /// </summary>
        [XmlAttribute("文本图像宽度")]
        public int text_image_width { get; set; }
        /// <summary>
        /// 文字位图的像素高度
        /// </summary>
        [XmlAttribute("文本图像高度")]
        public int text_image_height { get; set; }

        /// <summary>
        /// 打印模式 text image 字符打印或者图像打印
        /// </summary>
        [XmlAttribute("打印模式")]
        public string print_mode { get; set; }
        /// <summary>
        /// 图像打印模式左边距
        /// </summary>
        [XmlAttribute("图像左边距")]
        public int image_left_space { get; set; }
        /// <summary>
        /// 图像打印模式右边距
        /// </summary>
        [XmlAttribute("图像右边距")]
        public int image_right_space { get; set; }
        /// <summary>
        /// 字体名称
        /// </summary>
        [XmlAttribute("图像字体名称")]
        public string image_font_family_name { get; set; }
        /// <summary>
        /// 字体尺寸
        /// </summary>
        [XmlAttribute("图像字体大小")]
        public int image_font_size { get; set; }
        /// <summary>
        /// 对齐方式
        /// </summary>
        [XmlAttribute("文本打印对齐方式")]
        public string align { get; set; }
        public PrintTextFormatV1d0()
        {
            id = Guid.NewGuid();
            format_name = "print_text_forma_0";
            font_color_name = "Black";// "Red"
            is_double_height = false;
            is_double_width = false;
            is_bold = false;
            encoding_name = "Big5";
            text_image_height = 400;
            text_image_width = 24;
            image_left_space = 0;
            image_right_space = 0;
            print_mode = "Text";// "Image"
            align = "Left";//"Middle"  "Right"
            image_font_family_name = "宋体";
            image_font_size = 8;
        }
    }
}
